package com.mystudy.dataStructure.tree;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/8/6 22:37
 * @Description 线段树
 */
public class SegmentTree<E> {
    private E[] tree;
    private E[] data;
    private Merger<E> merger;

    public SegmentTree(E[] arr, Merger<E> merger) {
        data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        this.merger = merger;
        tree = (E[]) new Object[4 * arr.length];
        buildSegmentTree(0, 0, data.length - 1);

    }

    //在rootIndex的位置创建表示区间［Ｌ,R］的线段树,包括融合器接口
    private void buildSegmentTree(int rootIndex, int l, int r) {
        if (l == r) {
            tree[rootIndex] = data[l];
            return;
        }
        int leftTreeIndex = leftChild(rootIndex);
        int rightTreeIndex = rightChild(rootIndex);
        int mid = (l + r) / 2;
        //l,mid  mid+1,r
        buildSegmentTree(leftTreeIndex, l, mid);
        buildSegmentTree(rightTreeIndex, mid + 1, r);
        tree[rootIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
    }


    public int getSize() {
        return data.length;
    }

    //对于完全二叉树来说，可以使用数组表示，那么这一个节点的左孩子的表示
    private int leftChild(int index) {
        return 2 * index + 1;
    }

    private int rightChild(int index) {
        return 2 * index + 2;
    }
    //返回区间［queryL,queryR］的值
    public E query(int queryL,int queryR){
        if(queryL<0 || queryL>= data.length || queryR<0 || queryR>=data.length){
            throw new IllegalArgumentException("Index is illegal");
        }
        return query(0,0,data.length-1,queryL,queryR);
    }
    //在以rootIndex为根的线段树中[l...r]的范围里，搜索区间［queryL ... queryR］的值
    private E query(int rootIndex,int l,int r,int queryL,int queryR){
        if(l == queryL && r == queryR){
            return tree[rootIndex];
        }
        int mid = (l+r)/2;
        int leftTreeIndex = leftChild(rootIndex);
        int rightTreeIndex = rightChild(rootIndex);
        if(queryL >= mid+1){
            return query(rightTreeIndex,mid+1,r,queryL,queryR);
        }else if(queryR <= mid){
            return query(leftTreeIndex,l,mid,queryL,queryR);
        }
        E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
        E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
        E result = merger.merge(leftResult, rightResult);
        return result;
    }


    //将index位置的值，更新为e
    public void set(int index ,E e){
       if(index < 0 || index >=data.length){
           throw new IllegalArgumentException("Index is illegal");
       }
       data[index] = e;
       set(0,0,data.length-1,index,e);
    }

    private void set(int treeIndex,int l,int r,int index,E e){
        if(l == r){
            tree[treeIndex] = e;
            return;
        }
        int mid = (l+r)/2;
        int leftChild = leftChild(treeIndex);
        int rightChild = rightChild(treeIndex);
        if (index >= mid + 1) {
            set(rightChild,mid+1,r,index,e);
        }else{
            //index <= mid
            set(leftChild,l,mid,index,e);
        }
    }



    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Index is illegal");
        }
        return data[index];
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append('[');
        for (int i = 0; i < tree.length; i++) {
            if(tree[i] != null){
                res.append(tree[i]);
            }else{
                res.append("null");
            }
            if(i != tree.length-1){
                res.append(",");
            }
        }
        return res.toString();
    }
}
